package com.yc.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.study.entity.Announcement;
import com.yc.study.service.IAnnouncementService;
import com.yc.study.vo.AnnouncementVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 前端控制器
 *
 * @author 王妍
 * @since 2022-02-20
 */
@RestController
@RequestMapping("/study/announcement")
@AllArgsConstructor
public class AnnouncementController {
  private final IAnnouncementService announcementService;

  @GetMapping("/page")
  public Page<AnnouncementVo> page(Page<AnnouncementVo> page, AnnouncementVo announcement) {
    return announcementService.loadPage(page, announcement);
  }

  @GetMapping("{id}")
  public Announcement info(@PathVariable Long id){
    return announcementService.getById(id);
  }

  /**
   * 创建公告
   *
   * @param announcementVo 公告信息
   */
  @PostMapping
  public void save(@RequestBody AnnouncementVo announcementVo) {
    final Announcement announcement = new Announcement();
    BeanUtils.copyProperties(announcementVo, announcement);
    announcementService.saveOrUpdate(announcement);
  }

  /**
   * 删除公告
   *
   * @param announcementIds 公告ids
   */
  @DeleteMapping
  public void delete(@RequestBody Long[] announcementIds) {
    announcementService.removeBatchByIds(Arrays.asList(announcementIds));
  }
}
